<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <h1>请求百度的jsonp接口</h1>
    <input class="search" />
    <ul></ul>
    <script>
      function jsonp_1(res) {
        console.log(res.weather);
        document.querySelector("ul").innerHTML = "";
        res.weather.forEach((item) => {
          for (let key in item) {
            var liEle = document.createElement("li");
            liEle.innerHTML =key+':'+ item[key];
            document.querySelector("ul").appendChild(liEle);
          }

        let br=  document.createElement('br')
          liEle.appendChild(br)
        });
      }

      //  https://toy1.weather.com.cn/search?cityname=jn&callback=jsonp_2

      let searchEle = document.querySelector(".search");
      searchEle.oninput = function () {
        console.log(this.value);
        if (this.value !== "") {
          // 发送jsonp请求 数据
          let s = document.createElement("script");
          s.src = `https://query.asilu.com/weather/baidu?city=${this.value}&callback=jsonp_1`;
          document.body.appendChild(s);
        } else {
          document.querySelector("ul").innerHTML = "";
        }
      };
    </script>
  </body>
</html>
